package com.example.reference_book.util;

import com.example.reference_book.entity.Book;
import com.example.reference_book.filter.BookFilter;
import com.example.reference_book.filter.bookImpl.*;

import java.util.List;


public class BookSearchUtil {

    public static BookSearchUtil bookSearchUtil;
    public static List<Book> bookList;
    private BookSearchUtil (){

    }

    public void init(List<Book> bookList) {
        this.bookList = bookList;
    }

    public static BookSearchUtil getBookSearchUtil() {
        if (bookSearchUtil == null) {
            synchronized (BookSearchUtil.class) {
                if (bookSearchUtil == null) {
                    bookSearchUtil = new BookSearchUtil();
                }
            }
        }
        return bookSearchUtil;
    }

    public List<Book> searchByKey(int type, String key){
        BookFilter filter;
        switch (type){
            case 1:{
                filter = new BookNameFilter();
                break;
            }
            case 2:{
                filter = new BookAuthorFilter();
                break;
            }
            case 3:{
                filter = new BookTranslatorFilter();
                break;
            }
            case 4:{
                filter = new BookPublisherFilter();
                break;
            }
            default:
                filter = new BookAroundFilter();
        }
        return filter.getBooks(key,bookList);
    }

}
